# -*- coding: utf-8 -*-
import urllib,urllib2,cookielib
import os,sys,time
import re




def fetion(msg):
    url_login = 'http://f.10086.cn/im/login/inputpasssubmit1.action'
    url_logout = 'http://f.10086.cn/im/index/logoutsubmit.action?t='
    url_mymsg = 'http://f.10086.cn/im/user/sendMsgToMyselfs.action'
#    url_tomsg = 'http://f.10086.cn/im5/chat/sendNewShortMsg.action'
    url_invit = 'http://f.10086.cn/im/user/insertfriendsubmit.action'
    '''
    buddylist	1   好友列表
    localName	    无用
    nickname	3分100   最多5个中文或5个英文
    number	13705743215
    type	0   0是手机号
    '''
    
    
    user = '13957861878'
    password = '33g02g04'
    loginstatus = '4'
    arg_t = ''
    cj = cookielib.LWPCookieJar()
    opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
    urllib2.install_opener(opener)
    args = {'pass':password, 'm':user,'loginstatus':loginstatus}
    print 'Logining...'
    req = urllib2.Request(url_login, urllib.urlencode(args))
    jump = opener.open(req)
    page = jump.read();
    url = re.compile(r'<card id="start".*?ontimer="(.*?);').findall(page)[0] 
    arg_t = re.compile(r't=(\d*)').findall(page)[0]
    if url == '/im/login/login.action':
        print "error"
        return
    # sendmsg = urllib2.Request(url_mymsg, urllib.urlencode({'msg':msg}))
    sendmsg = urllib2.Request(url_invit, urllib.urlencode({'buddylist':'1','localName':'这是测试帐号','nickname':'违章3分','number':'13705743215','type':'0'}))
    finish = urllib2.urlopen(sendmsg)

    # if finish.geturl == 'http://f.10086.cn/im/user/sendMsgToMyself.action' :
    if finish.geturl == url_invit :
        print 'Send Failed!'
    else:
        print 'Send Successfully'
    logout = urllib2.Request(url_logout + arg_t)
    response = urllib2.urlopen(logout) 
    print 'Logout Successfully!'
    print response.read().decode('utf-8').encode('gbk')
fetion('aaa')